package org.ns.tomdog.search;

import java.util.HashMap;
import java.util.Map;

public class SearchNode {

    private char c;
    private Object value;
    private SearchNode parent;

    private Map<Character, SearchNode> nexts = new HashMap<>();

    public SearchNode(char c, SearchNode parent) {
        this.c = c;
        this.parent = parent;
    }

    SearchNode getOrCreate(char c, SearchNode parent) {
        if (c == '*' && isWildcard()) {
            return this;
        }
        SearchNode searchNode = nexts.get(c);
        if (searchNode == null) {
            searchNode = new SearchNode(c, parent);
            nexts.put(c, searchNode);
        }
        return searchNode;
    }

    SearchNode getNext(char c) {
        SearchNode next = nexts.get(c);
        if (next == null) {
            if (isWildcard()) {
                if (nexts.size() == 0) {
                    return null;
                } else {
                    return this;
                }
            } else {
                next = nexts.get('*');
            }
        }
        return next;
    }
    // 12312223
    // 123*123
    // 123*223

    /**
     * 是通配符
     * @return
     */
    public boolean isWildcard() {
        return c == '*';
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}
